package com.exrobot.module.rbt.controller.admin.robotfiles;

import org.springframework.web.bind.annotation.*;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import jakarta.validation.constraints.*;
import jakarta.validation.*;
import jakarta.servlet.http.*;
import java.util.*;
import java.io.IOException;

import com.exrobot.framework.common.pojo.PageParam;
import com.exrobot.framework.common.pojo.PageResult;
import com.exrobot.framework.common.pojo.CommonResult;
import com.exrobot.framework.common.util.object.BeanUtils;
import static com.exrobot.framework.common.pojo.CommonResult.success;

import com.exrobot.framework.excel.core.util.ExcelUtils;

import com.exrobot.framework.apilog.core.annotation.ApiAccessLog;
import static com.exrobot.framework.apilog.core.enums.OperateTypeEnum.*;

import com.exrobot.module.rbt.controller.admin.robotfiles.vo.*;
import com.exrobot.module.rbt.dal.dataobject.robotfiles.RobotFilesDO;
import com.exrobot.module.rbt.service.robotfiles.RobotFilesService;

@Tag(name = "管理后台 - 机器人文件")
@RestController
@RequestMapping("/rbt/robot-files")
@Validated
public class RobotFilesController {

    @Resource
    private RobotFilesService robotFilesService;

    @PostMapping("/create")
    @Operation(summary = "创建机器人文件")
    @PreAuthorize("@ss.hasPermission('rbt:robot-files:create')")
    public CommonResult<Long> createRobotFiles(@Valid @RequestBody RobotFilesSaveReqVO createReqVO) {
        return success(robotFilesService.createRobotFiles(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新机器人文件")
    @PreAuthorize("@ss.hasPermission('rbt:robot-files:update')")
    public CommonResult<Boolean> updateRobotFiles(@Valid @RequestBody RobotFilesSaveReqVO updateReqVO) {
        robotFilesService.updateRobotFiles(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除机器人文件")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('rbt:robot-files:delete')")
    public CommonResult<Boolean> deleteRobotFiles(@RequestParam("id") Long id) {
        robotFilesService.deleteRobotFiles(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除机器人文件")
                @PreAuthorize("@ss.hasPermission('rbt:robot-files:delete')")
    public CommonResult<Boolean> deleteRobotFilesList(@RequestParam("ids") List<Long> ids) {
        robotFilesService.deleteRobotFilesListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得机器人文件")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('rbt:robot-files:query')")
    public CommonResult<RobotFilesRespVO> getRobotFiles(@RequestParam("id") Long id) {
        RobotFilesDO robotFiles = robotFilesService.getRobotFiles(id);
        return success(BeanUtils.toBean(robotFiles, RobotFilesRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得机器人文件分页")
    @PreAuthorize("@ss.hasPermission('rbt:robot-files:query')")
    public CommonResult<PageResult<RobotFilesRespVO>> getRobotFilesPage(@Valid RobotFilesPageReqVO pageReqVO) {
        PageResult<RobotFilesDO> pageResult = robotFilesService.getRobotFilesPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, RobotFilesRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出机器人文件 Excel")
    @PreAuthorize("@ss.hasPermission('rbt:robot-files:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportRobotFilesExcel(@Valid RobotFilesPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<RobotFilesDO> list = robotFilesService.getRobotFilesPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "机器人文件.xls", "数据", RobotFilesRespVO.class,
                        BeanUtils.toBean(list, RobotFilesRespVO.class));
    }

}